package test033;

import org.apache.ibatis.datasource.pooled.PooledDataSource;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;

import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;

public class App2 {

    public static void main( String[] args ) {

        SqlSessionFactory factory = null;
        try {
            InputStream inputStream = Resources.getResourceAsStream("config.xml");
            SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
            //方法1：采用xml配置文件生成  【店长推荐】
            factory = builder.build(inputStream);
            System.out.println("2.建造工厂"+factory);
        } catch (Exception e) {
            e.printStackTrace();
        }


        SqlSession sqlSession = factory.openSession();
        System.out.println("3.session："+sqlSession);


        //方案1：
        StudentDao studentDao = sqlSession.getMapper(StudentDao.class);
        Student s = new Student();
        s.setStudId(8);
        s.setName("蔡大姐");
        s.setEmail("1234567@qq.com");
        System.out.println("student e-mail:"+ s.getEmail() );
        studentDao.updateStudent(  s  );

        s = studentDao.findStudentById( s.getStudId()  );


        System.out.println("new student id:"+ s.getStudId() );
        System.out.println("new student email:"+s.getEmail());

        sqlSession.commit();
        sqlSession.close();


        //.....................
        //方案2：
        //Student student2 = sqlSession.selectOne("test033.StudentDao.findStudentById", 1);




    }
}
